package joinmessage

import (
	"admin.supervise.limaochuxing.top/admin/schemas/req"
	"admin.supervise.limaochuxing.top/admin/schemas/resp"
	"admin.supervise.limaochuxing.top/core/request"
	"admin.supervise.limaochuxing.top/core/response"
	"admin.supervise.limaochuxing.top/model/joinmessage"

	"gorm.io/gorm"
)

// IJoinMessageService 登记信息服务接口
type IJoinMessageService interface {
	// List 获取登记信息列表
	List(page request.PageReq, listReq req.JoinMessageListReq) (res response.PageResp, e error)
	// Detail 获取登记信息详情
	Detail(id int64) (res resp.JoinMessageResp, e error)
	// Follow 跟进登记信息
	Follow(followReq req.JoinMessageFollowReq) (e error)
	// UpdateRemark 更新备注信息
	UpdateRemark(remarkReq req.JoinMessageRemarkReq) (e error)
}

// joinMessageService 登记信息服务实现
type joinMessageService struct {
	db *gorm.DB
}

// NewJoinMessageService 创建登记信息服务实例
func NewJoinMessageService(db *gorm.DB) IJoinMessageService {
	return &joinMessageService{db: db}
}

// List 获取登记信息列表
func (s *joinMessageService) List(page request.PageReq, listReq req.JoinMessageListReq) (res response.PageResp, e error) {
	limit := page.PageSize
	offset := page.PageSize * (page.PageNo - 1)

	var total int64
	var list []struct {
		joinmessage.JoinMessage
		Username string `json:"username"`
		Nickname string `json:"nickname"`
	}

	db := s.db.Model(&joinmessage.JoinMessage{}).Select("la_join_message.*, la_system_auth_admin.username, la_system_auth_admin.nickname").Joins("LEFT JOIN la_system_auth_admin ON la_join_message.follow_id = la_system_auth_admin.id")

	// 添加查询条件
	if listReq.Name != "" {
		db = db.Where("la_join_message.name LIKE ?", "%"+listReq.Name+"%")
	}
	if listReq.Mobile != "" {
		db = db.Where("la_join_message.mobile LIKE ?", "%"+listReq.Mobile+"%")
	}
	// if listReq.Source > 0 {
	//     db = db.Where("la_join_message.source = ?", listReq.Source)
	// }
	// if listReq.Status >= 0 {
	//     db = db.Where("la_join_message.status = ?", listReq.Status)
	// }
	// if listReq.RegisterTime != "" {
	//     db = db.Where("DATE(la_join_message.register_time) = ?", listReq.RegisterTime)
	// }

	// 统计总数
	err := db.Count(&total).Error
	if e = response.CheckErr(err, "List Count err"); e != nil {
		return
	}

	// 获取列表数据
	err = db.Limit(limit).Offset(offset).Order("la_join_message.id desc").Find(&list).Error
	if e = response.CheckErr(err, "List Find err"); e != nil {
		return
	}

	var respList []resp.JoinMessageResp
	response.Copy(&respList, list)

	return response.PageResp{
		Lists:    respList,
		Count:    total,
		PageNo:   page.PageNo,
		PageSize: page.PageSize,
	}, nil
}

// Detail 获取登记信息详情
func (s *joinMessageService) Detail(id int64) (res resp.JoinMessageResp, e error) {
	var info joinmessage.JoinMessage

	err := s.db.Where("id = ?", id).First(&info).Error
	if e = response.CheckErr(err, "Detail First err"); e != nil {
		return
	}

	response.Copy(&res, info)
	return
}

// Follow 跟进登记信息
func (s *joinMessageService) Follow(followReq req.JoinMessageFollowReq) (e error) {
	// 更新跟进信息
	err := s.db.Model(&joinmessage.JoinMessage{}).Where("id = ?", followReq.Id).Updates(map[string]interface{}{
		"status":    int8(1), // 已跟进
		"follow_id": followReq.FollowId,
	}).Error

	if e = response.CheckErr(err, "Follow Updates err"); e != nil {
		return
	}
	return
}

// UpdateRemark 更新备注信息
func (s *joinMessageService) UpdateRemark(remarkReq req.JoinMessageRemarkReq) (e error) {
	err := s.db.Model(&joinmessage.JoinMessage{}).
		Where("id = ?", remarkReq.Id).
		Update("remark", remarkReq.Remark).Error
	if e = response.CheckErr(err, "UpdateRemark Update err"); e != nil {
		return
	}
	return
}
